package com.boboPlanet.comm.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO MQ 生产者确认回调配置
 * @Author: mis_wu
 * @Date: 2023/6/26
 * @Company planet
 **/
@Slf4j
@Configuration
public class MqReturnCallbackConfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);

        rabbitTemplate.setReturnCallback(((message, replyCode, replyText, exchange, routingKey) -> {
            log.error("消息发送失败,应答码: {}, 原因: {}, 交换机: {}, 路由key: {}",replyCode,replyText,exchange,routingKey);

            //消息重发，或告知其他服务失败了
        }));

    }
}
